<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<div class="execution">
				<basic-container>
					<avue-crud
						ref="crud"
						v-model="form"
						v-model:page="page"
						:data="tableData"
						:table-loading="tableLoading"
						:option="tableOption"
						:before-open="handleOpenBefore"
						:permission="permissionList"
						@on-load="getList"
						@search-change="searchChange"
						@refresh-change="refreshChange"
						@size-change="sizeChange"
						@current-change="currentChange"
						@row-update="handleUpdate"
						@row-save="handleSave"
						@row-del="rowDel"
						@search-reset="resetsearch"
					>
						<template #menu-left="{}">
							<el-button v-if="permissions.access_accessFloor_add" class="filter-item" type="primary" icon="el-icon-edit" @click="$refs.crud.rowAdd()"
								>添加
							</el-button>
							<el-button
								v-if="permissions.access_accessFloor_import"
								class="filter-item"
								plain
								type="primary"
								icon="el-icon-upload"
								@click="$refs.excelUpload.show()"
								>导入
							</el-button>
							<!-- <el-button 
					v-if="permissions.access_accessFloor_export"
					class="filter-item" 
					plain type="primary" 
					icon="el-icon-download" 
					@click="exportExcel">导出
					</el-button> -->
						</template>
						<template #menu-right="{}">
							<el-button class="filter-item" v-auth="'access_accessFloor_export'" circle icon="Download" @click="exportExcel"></el-button>
						</template>
						<template #floorPid-form>
							<avue-select
								v-model="form.floorPid"
								placeholder="请选择建筑"
								:dic="buildingsOptions"
								:props="buildingsProps"
								@change="placenoChange"
							></avue-select>
						</template>
						<template #floorPid-search>
							<avue-select
								v-model="searchFloorPid"
								placeholder="请选择 建筑"
								:dic="buildingsOptions"
								:props="buildingsProps"
								@keyup.enter="searchChange"
							></avue-select>
						</template>
						<template #floorCn-search>
							<avue-input v-model="searchForm.floorCn" placeholder="请输入 楼层中文名" @keyup.enter="searchChange"></avue-input>
						</template>
					</avue-crud>
					<!--excel 模板导入 -->
					<excel-upload
						ref="excelUpload"
						title="楼层信息导入"
						url="/bm/bamsfloor/import"
						temp-name="楼层信息.xlsx"
						temp-url="/bm/sys-file/local/file/floor.xlsx"
						@refreshDataList="handleRefreshChange"
					></excel-upload>
				</basic-container>
			</div>
		</div>
	</div>
</template>

<script>
import { fetchList, addObj, putObj, delObj, getBamSFloorCode } from '/@/api/basiciot/area/floor';
import { getAllBuildings } from '/@/api/basiciot/area/buildings';
import { tableOption } from '/@/crud/basiciot/area/floor';
import { getAuthBtnList } from '/@/utils/btnpermissions';

import ExcelUpload from '/@/components/ExcelUpload/index.vue';

import other from '/@/utils/other';

export default {
	name: 'bamfloor',
	components: { ExcelUpload },
	data() {
		return {
			searchForm: {},
			searchFloorPid: '',
			tableData: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 10, // 每页显示多少条
			},
			tableLoading: false,
			tableOption: tableOption,
			buildingsOptions: [],
			buildingsProps: {
				label: 'buildingsCn',
				value: 'buildingsId',
			},
			form: {},
			dic: {},
			last_unitno: '',
			front_unitno: '',
			isupdate: true,
		};
	},
	mounted() {
		getAllBuildings({}).then((res) => {
			this.buildingsOptions = res.data;
			this.dic = new Map();
			res.data.forEach((d) => {
				this.dic.set(d.buildingsId, d.buildingsCn);
			});
		});
	},
	computed: {
		permissions() {
			return {
				access_accessFloor_view: getAuthBtnList('access_accessFloor_view'),
				access_accessFloor_add: getAuthBtnList('access_accessFloor_add'),
				access_accessFloor_edit: getAuthBtnList('access_accessFloor_edit'),
				access_accessFloor_del: getAuthBtnList('access_accessFloor_del'),
				access_accessFloor_export: getAuthBtnList('access_accessFloor_export'),
				access_accessFloor_import: getAuthBtnList('access_accessFloor_import'),
			};
		},
		permissionList() {
			return {
				addBtn: getAuthBtnList('access_accessFloor_add'),
				delBtn: getAuthBtnList('access_accessFloor_del'),
				editBtn: getAuthBtnList('access_accessFloor_edit'),
			};
		},
	},
	methods: {
		resetsearch() {
			this.searchForm = {};
			this.searchFloorPid = '';
		},
		handleOpenBefore(show, type) {
			getAllBuildings({}).then((res) => {
				this.buildingsOptions = res.data;
				this.dic = new Map();
				res.data.forEach((d) => {
					this.dic.set(d.buildingsId, d.buildingsCn);
				});
			});
			if (type === 'edit') {
				this.last_unitno = this.form.floorCode && this.form.floorCode.substr(this.form.floorPid.length, 4);
				this.isupdate = false;
			} else if (type === 'add') {
				this.last_unitno = '';
			}
			show();
		},
		getList(page, params) {
			this.tableLoading = true;
			fetchList(
				Object.assign(
					{
						current: page.currentPage,
						size: page.pageSize,
					},
					params,
					this.searchForm
				)
			)
				.then((response) => {
					this.tableData = response.data.records;
					this.page.total = response.data.total;
					this.tableLoading = false;
				})
				.catch(() => {
					this.tableLoading = false;
				});
			// this.tableData=[{
			//     floorId:'1',
			//     floorPid:1,
			//     floorCn:'1层',
			//     floorEn:'oneFloor',
			//     floorCode:'123465',
			//     desc:'测试'
			// }]
			// this.page.total=1
		},
		rowDel: function (row, index) {
			this.$confirm('是否确认删除名称为' + row.floorCn, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delObj(row.floorId);
				})
				.then((data) => {
					this.$message.success('删除成功');
					this.getList(this.page);
				})
				.catch((cancelorerror) => {});
		},
		handleUpdate: function (row, index, done, loading) {
			//row.floorCode = this.front_unitno + this.last_unitno
			putObj(row)
				.then((data) => {
					this.$message.success('修改成功');
					done();
					this.getList(this.page);
				})
				.catch(() => {
					loading();
				});
		},
		handleSave: function (row, done, loading) {
			//row.floorCode = this.front_unitno + this.last_unitno
			addObj(row)
				.then((data) => {
					this.$message.success('添加成功');
					done();
					this.getList(this.page);
				})
				.catch(() => {
					loading();
				});
		},
		sizeChange(pageSize) {
			this.page.pageSize = pageSize;
		},
		currentChange(current) {
			this.page.currentPage = current;
		},
		searchChange(form, done) {
			if (this.searchFloorPid) {
				form.floorPid = this.searchFloorPid;
				this.searchForm.floorPid = this.searchFloorPid;
			} else {
				delete this.searchForm.floorPid;
			}
			//this.searchForm = form
			this.page.currentPage = 1;
			this.getList(this.page, form);
			done();
		},
		refreshChange() {
			this.getList(this.page);
		},
		placenoChange(data) {
			//this.front_unitno = data.value;
			if (data.value) {
				if (this.isupdate) {
					getBamSFloorCode({
						code: data.value,
						level: 3,
						suffix: '',
						binary: 1,
					}).then((res) => {
						this.form.floorCode = res.data;
					});
				}
				this.isupdate = true;
			}
		},
		handleRefreshChange() {
			this.getList(this.page);
		},
		exportExcel() {
			other.downBlobFile(other.adaptationUrl('/admin/accessFloor/export'), this.searchForm, '楼层信息.xlsx');
		},
	},
};
</script>
<style lang="scss" scoped>
.span-title {
	display: flex;
	background: rgb(226, 219, 219);
	border-radius: 4px;

	.span-label {
		padding-left: 10px;
		color: #fff;
		height: 36px;
		flex: 2;
	}

	.span-input {
		flex: 2;
	}
}
</style>
